// 类型定义扩展
import {
  Permission,
  usePermission,
  UserInfo
} from '@/composables/usePermission';
import type { Plugin } from 'vue';

declare module '@vue/runtime-core' {
  interface ComponentCustomProperties {
    $hasPermission: (_permission: Permission) => boolean;
    $hasAnyPermission: (_permissions: Permission[]) => boolean;
    $hasRole: (_role: UserInfo['role']) => boolean;
  }
}

export interface PermissionPluginOptions {
  strict?: boolean;
  validate?: (_permission: Permission, _user: UserInfo) => boolean;
}

export const PermissionPlugin: Plugin<PermissionPluginOptions> = {
  install(app, options) {
    const permission = usePermission(options);

    // 全局属性
    app.config.globalProperties.$hasPermission = permission.hasPermission;
    app.config.globalProperties.$hasAnyPermission = permission.hasAnyPermission;
    app.config.globalProperties.$hasRole = permission.hasRole;
  }
};
